<template>
  <div>
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb> -->

    <el-card style="margin-top: 10px;">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <div class="manager-all">
          <div class="project-manager"></div>
          <div class="project-item">
            <el-breadcrumb-item :to="{ path: '/' }"
              >用户管理
            </el-breadcrumb-item>
          </div>
        </div>
        <hr
          style="width: 1642px;
                 height: 1px;
                 border:none;
                 background: #E8ECF0;
                 margin-left:-23px;"
        />
      </el-breadcrumb>
      <el-row>
        <el-col :span="5">
          <el-button
            type="primary"
            @click="addFormVisible = true"
            class="add el-icon-plus"
            style="margin-top:10px;"
            size="medium"
          >
            <!-- <img src="../../icon标/添加数据.png" class="addimg" alt="" /> -->
            添加新用户
          </el-button>
        </el-col>
        <!--        <el-col :span="10">-->
        <!--          <el-input-->
        <!--            v-model="query"-->
        <!--            clearable-->
        <!--            placeholder="搜索想要查找的内容"-->
        <!--            @clear="getUserList"-->
        <!--            class="input"-->
        <!--          >-->
        <!--            <el-button-->
        <!--              slot="append"-->
        <!--              icon="el-icon-search"-->
        <!--              @click="getUserList"-->
        <!--            ></el-button>-->
        <!--          </el-input>-->
        <!--        </el-col>-->
        <el-col :span="1"></el-col>
      </el-row>

      <!--      用户列表区域-->
      <el-table
        :data="userList"
        stripe
        style="width:100%;margin-top:20px;"
        class="bioTable"
        :header-cell-style="{ background: '#F6F7FB' }"
      >
        <!-- 索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="手机号码" prop="phoneNumber"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <!--        <el-table-column-->
        <!--          label="权限"-->
        <!--          prop="inputDate"-->
        <!--        ></el-table-column>-->
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <div class="btn-all">
              <div class="bj">
                <!-- <i class="el-icon-edit-outline" style="color:#409EFF;"></i> -->
                <!--                进入新页面/新模块-->
                <img src="../../icon标/查看 (1).png" alt="" />
                <router-link
                  :underline="false"
                  style="margin-left:5px;font-size:14px;cursor: pointer;margin-top:-5px;"
                  :to="{path:'/powerManaged/'+scope.row.id,query:{Upage:queryInfo.page,Usize:queryInfo.size}}"
                  >权限详情
                </router-link>
              </div>
              <div class="sx"></div>
              <div class="sc">
                <!-- <i class="el-icon-delete" style="color:#F56C6C;"></i> -->
                <img src="../../icon标/删除.png" alt="" />
                <el-link
                  type="danger"
                  :underline="false"
                  @click="removeUser(scope.row.id)"
                  style="margin-left:5px;font-size:14px;cursor: pointer;margin-top:-5px;"
                  >删除
                </el-link>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--            分页区域-->
      <el-pagination
        background
        :current-page="queryInfo.page"
        :page-size="queryInfo.size"
        :page-sizes="[5, 10, 15, 20]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      :visible.sync="addFormVisible"
      title="添加新用户"
      width="800px"
      @close="addDialogClosed"
    >
      <hr
        style=" margin-top:-30px;border:none;background:#4087fc;height:1.5px;"
      />
      <hr
        style="margin-top:-9px;border:none;height:3px;width:100px;background:#4087fc;margin-left:0px;"
      />
      <el-form
        :model="addForm"
        ref="addUserRef"
        label-width="140px"
        class="molecular-design"
        :rules="addUserRules"
        style="margin-left:-60px;margin-top:50px;"
      >
        <el-row style="margin-top:30px;">
          <el-row>
            <el-col :span="12">
              <el-form-item label="用户名 :" prop="provider">
                <el-input v-model="addForm.username"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="密码 :" prop="provider">
                <el-input v-model="addForm.password"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱 :" prop="provider">
              <el-input v-model="addForm.email"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码 :" prop="provider">
              <el-input v-model="addForm.phoneNumber"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--      底部区域-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false" plain>取 消</el-button>
        <el-button
          type="primary"
          @click="addUser"
          style="margin-left:50px;margin-bottom:15px;"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "AddNewUser",
  data() {
    return {
      userList: [],
      powerList: [],
      addFormVisible: false,
      powerVisible: false,
      addForm: {
        username: "",
        password: "",
        email: "",
        phoneNumber: ""
      },
      queryInfo: {
        // query: "",
        // 当前的页数
        page: 1,
        // 当前每页显示多少条数据
        size: 5
      },
      total: 0,
      //搜索用户名
      // query: '',
      addUserRules: {}
    };
  },
  created() {
    var query=this.$route.query
    if(query.Upage!=null){
      this.queryInfo.page=Number(query.Upage)
    }
    if(query.Usize!=null){
      this.queryInfo.size=Number(query.Usize)
    }
    this.getUserList();
  },
  methods: {
    async getUserList() {
      // if (this.query === "") {
      //   this.queryInfo.query = -1;
      // } else {
      //   this.queryInfo.query = this.query;
      // }
      // console.log(this.queryInfo.query)
      const { data: res } = await this.$axios.get("userManager/getUser", {
        params: this.queryInfo
      });
      if (res.code !== 200) {
        return this.$message.error("获取角色列表失败！");
      }
      this.userList = res.data;
      this.total = res.total;
      console.log(this.userList);
    },
    //关闭表单的时候 重设内部属性为空
    addDialogClosed() {
      this.$refs.addUserRef.resetFields();
    },
    addUser() {
      this.$refs.addUserRef.validate(async valid => {
        console.log(this.addForm);
        if (!valid) return;
        // valid为True 此时可以发起添加用户的网络请求
        //第一个参数是路径 第二个参数提交的data
        const { data: res } = await this.$axios.post(
          "userManager/addUser",
          this.addForm
        );
        console.log(res);
        if (res.code !== 200) {
          this.$message.error("添加用户失败！");
        }
          this.$message.success("添加用户成功！");

        // 隐藏添加用户的对话框
        this.addFormVisible = false;
        // 重新获取用户的列表
        await this.getUserList();
      });
    },
    // resetSelectInput() {
    //   this.query = ''
    // },

    async removeUser(id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
          // 如果取消 会抛出一个错误error 这里用catch捕获他 然后返回error
          // 可以简写成.catch(err => err)
        }
      ).catch(error => {
        return error;
      });
      // 如果用户确认删除，返回值为字符串 confirm
      // 如果用户取消删除，返回值为字符串 cancel
      if (confirmResult !== "confirm") {
        return this.$message.info("以取消删除");
      }
      // console.log('已删除用户')
      const { data: res } = await this.$axios.delete(
        "userManager/deleteUser?id=" + id
      );
      console.log(res);
      if (res.code !== 200) {
        return this.$message.error("删除用户失败！");
      }
      this.$message.success("删除用户成功!");
      await this.getUserList();
    },
    handleSizeChange(newSize) {
      this.$router.push({path:'/userManaged',query:{Upage:this.queryInfo.page,Usize:this.queryInfo.size}})
      this.queryInfo.size = newSize;
      this.getUserList();
      console.log(`每页 ${newSize} 条`);
    },
    // 监听 分页的page 改变的事件
    handleCurrentChange(newPage) {
      this.$router.push({path:'/userManaged',query:{Upage:this.queryInfo.page,Usize:this.queryInfo.size}})
      this.queryInfo.page = newPage;
      this.getUserList();
      console.log(`当前页: ${newPage}`);
    }
  }
};
</script>

<style>
.bioTable {
  margin-top: 20px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  /*width: 90px;*/
  color: #99a9bf;
}

.demo-table-expand {
  margin-right: 0;
  margin-bottom: 0;
}

.expand-msg {
  border-radius: 1px !important;
  border: 1px solid rgba(239, 239, 239, 0.36);
  /*height: 50px;*/
  line-height: 50px;
  /*border: 1px solid #000 !important;*/
  /*border-right: none;*/
}

.expand-msg-left {
  border-left: 2px solid rgba(239, 239, 239, 0.36);
  /*border-left: 2px solid #d7dae2;*/
}

.table-item {
  margin-bottom: 0px !important;
  line-height: 50px;
  padding-left: 10px;
}

.table-item-span {
  line-height: 50px;
}

.add-button {
  margin-top: 5px;
}

.table-image {
  width: auto;
  height: auto;
  /*max-width: 50%;*/
  /*max-height: 50%;*/
  display: block;
}
.manager-all {
  margin-top: 6px;
  display: flex;
  height: 30px;
  width: 100px;
  justify-content: center;
  margin-bottom: 30px;
}

.project-manager {
  width: 4px;
  height: 30px;
  background: #4087fc;
}

.project-item {
  width: 72px;
  height: 18px;
  font-size: 18px;
  font-family: Microsoft YaHei;
  font-weight: 600px;
  color: #333333;
  margin-left: 10px;
  line-height: 30px;
}

/*.el-form-item .table-item label{*/
/*  line-height: 50px;*/
/*  border-left: 2px solid #d7dae2;*/
/*  padding-left: 10px;*/
/*}*/
/*.el-form-item .demo-table-expand{*/
/*  margin-bottom: 0px !important;*/
/*}*/

/*.el-form-item__content{*/
/*  line-height: 50px;*/
/*  border-left: 2px solid #d7dae2;*/
/*  padding-left: 10px;*/
/*}*/
/*.el-form-item__label{*/
/*  padding-left: 10px;*/
/*}*/
.btn-all {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.cell {
  text-align: center;
}

.sx {
  width: 1.5px;
  height: 18px;
  /* font-size: 14px;
  font-weight: 400; */
  background: #ccccccff;
}

.addimg {
  width: 22px;
  height: 16px;
  margin-left: 0px;
  margin-top: 0;
  float: left;
}
a {
  text-decoration: none;
  color: #2bbb7f;
}
.Biosearch {
  margin-top: 25px;
  background: rgba(247, 247, 247, 1);
  display: flex;
  height: 100px;
  align-items: center;
  padding: 0 30px;
  justify-content: space-between;
}
</style>
